<template>
	<!-- 添加地址 -->
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.consignee" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" v-model="addressData.phone" placeholder="收货人手机号码" placeholder-class="placeholder" />
		</view>
		<view class="row b-b"> 
			<text class="tit">地址</text>
			<pickerAddress @change="change">{{txt}}</pickerAddress>
		</view>
		<view class="row b-b">
			<text class="tit">详细</text>
			<input class="input" type="text" v-model="addressData.address" placeholder="楼号、门牌" placeholder-class="placeholder" />
		</view>

		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.defaultAddress" color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm(item)">提交</button>
	</view>
</template>

<script>
	import pickerAddress from '../wangding-pickerAddress/wangding-pickerAddress.vue'
	export default {
		data() {
			return {
				text: '',
				urlid:'',
				nuar:'',
				txt: '选择地址',
				addres: '',
				addressData: {
					consignee: '',
					phone: '',
					address: '',
					defaultAddress: false
				}
			}
		},
		components: {
			pickerAddress
		},
		onShow(){
			if (uni.getStorageSync('accessToken') == '') {
				uni.showModal({
					title: '登录提示',
					content: '您尚未登录，是否立即登录',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '../long/long'
							})
							return
						} else if (res.cancel) {
							// console.log('用户点击取消');
						}
					}
				});
			}
		},
		onLoad(option) {
			this.nuar = option.name
			if (option.type === 'edit') {
				this.addressData = JSON.parse(decodeURIComponent(option.data))
				this.txt = this.addressData.province + this.addressData.city + this.addressData.county
				this.urlid = this.addressData.id
				
				this.addres = [this.addressData.province,this.addressData.city,this.addressData.county]
			}
			this.manageType = option.type;
		},
		methods: {
			change(data) {
				this.txt = data.data.join('')
				this.addres = data.data
				// console.log(this.addres+'11111111')
			},
			switchChange(e) {
				this.addressData.defaultAddress = !this.addressData.defaultAddress
				if (this.addressData.defaultAddress == false) {
					this.addressData.defaultAddress = 0
				} else {
					this.addressData.defaultAddress = 1
				}
			},
			//提交
			confirm(e) {
				let _this = this
				console.log(this.nuar)
				if (this.nuar == '新增') {
					let data = this.addressData;
					console.log(111111)
					console.log(this.addres[0], this.addres[1], this.addres[2], data.address, data.defaultAddress, data.phone, data.consignee)
					uni.request({
						url: `http://192.168.1.54:8081/m.api`,
						method:"GET",
						data:{
							"_gp":"address",
							"_mt":"addAddress",
							"province":this.addres[0],
							"city":this.addres[1],
							"county":this.addres[2],
							address:data.address,
							"defaultAddress":data.defaultAddress,
							"phone":data.phone,
							"consignee":data.consignee
						},
						header: {
							'accesstoken': uni.getStorageSync('accessToken')
						},
						success: function(res) {
							// console.log(res)
							if (res.data.errmsg == "成功") {
								uni.showToast({
									title: '添加地址成功',
									success: () => {
										setTimeout(() => {
											uni.navigateBack();
										}, 1000)
									}
								})
							} else {
								_this.text = res.data.errmsg
							}
						}
					})
				} else {
					let _this = this
					console.log(_this.addres)
					let data = this.addressData;
					uni.request({
						url:`http://192.168.1.54:8081/m.api?_gp=address&_mt=updateAddress&addressId=${data.id}&province=${_this.addres[0]}&city=${_this.addres[1]}&county=${_this.addres[2]}&address=${data.address}&defaultAddress=${data.defaultAddress}&phone=${data.phone}&consignee=${data.consignee}`,
						header: {
							'accesstoken': uni.getStorageSync('accessToken')
						},
						success: function(res) {
							console.log(res)
							if (res.data.errmsg == "成功") {
								uni.showToast({
									title: '修改地址成功',
									success: () => {
										console.log(data.defaultAddress)
										setTimeout(() => {
											uni.navigateBack();
										}, 1000)
									}
								})
							} else {
								_this.text = res.data.errmsg
							}
						}
					})
				}
			}
		}
	}
</script>

<style scoped="scoped">
	page {padding-top: 16rpx;}
	.row {display: flex; align-items: center; position: relative; padding: 0 30rpx; height: 110rpx; background: #fff; border-bottom: 3rpx solid #F5F5F5;}
	.row .input {flex: 1; font-size: 30rpx;}
	.row .icon-shouhuodizhi {font-size: 36rpx;}
	.row .tit {flex-shrink: 0; width: 120rpx; font-size: 30rpx;}
	.default-row {margin-top: 16rpx;
	}
	.default-row .tit {flex: 1;}
	.default-row switch {transform: translateX(16rpx) scale(.9);}
	.add-btn {display: flex; align-items: center; justify-content: center; width: 690rpx; height: 80rpx; margin: 60rpx auto; background-color: #fa436a;
		color: #fff; border-radius: 10rpx; box-shadow: 2rpx 4px 20rpx rgba(219, 63, 96, 0.4);}
</style>
